package com.springboot.application.controller;

import com.springboot.application.entity.AreaEntity;
import com.springboot.application.enums.ResultCodeEnum;
import com.springboot.application.exceptions.ApiException;
import com.springboot.application.response.AreaInfoResponse;
import com.springboot.application.service.AreaService;
import org.apache.ibatis.annotations.Param;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhengzihong
 * @version 1.0.0
 * @date 2018/10/31
 * @Description
 */
@RestController
public class AreaController {

    @Resource
    private AreaService areaService;


    @RequestMapping(value = "/getAreaInfo",method = RequestMethod.GET)
    public List<AreaInfoResponse> getAreaInfo(@Param(value = "cityName") String cityName){
        AreaEntity areaEntity = new AreaEntity();
        areaEntity.setCityName(cityName);
        List<AreaEntity> areaEntities = areaService.findAreaInfoList(areaEntity);
        if (CollectionUtils.isEmpty(areaEntities)) {
           throw new ApiException(ResultCodeEnum.RESULT_IS_NULL);
        }
        return areaEntities.stream().map(entity -> {
            AreaInfoResponse areaInfoResponse = new AreaInfoResponse();
            areaInfoResponse.setProvinceName(entity.getProvinceName());
            areaInfoResponse.setCityName(entity.getCityName());
            return areaInfoResponse;
        }).collect(Collectors.toList());
    }


}
